//
//  MenuScene.m
//  CandyJump
//
//  Created by Jordy on 11年3月27日.
//  Copyright 2011 Peru Republic. All rights reserved.
//

#import "MenuScene.h"
#import "GameScene.h"
#import "ShopScene.h"
#import	"OptionScene.h"
#import	"AdMobView.h"
#import	"AdDelegate.h"
#import "SimpleAudioEngine.h"

@implementation MenuScene

+ (id) scene
{
	CCScene *scene = [CCScene node];
	
	MenuScene *layer = [MenuScene node];
	
	[scene addChild: layer];
	
	return scene;
}

- (id) init
{
	if( (self=[super init] ))
	{
		CCSprite *background = [CCSprite spriteWithFile:@"Default.png"];
		background.anchorPoint = CGPointZero;
		background.opacity = 50;
		
		[self addChild:background z:0 tag:1];
		
		CCMenuItem *playButton = [CCMenuItemFont itemFromString:@"Play" target:self selector:@selector(gotoGameScene:)];
		CCMenuItem *shopButton = [CCMenuItemFont itemFromString:@"Shop" target:self selector:@selector(gotoShopScene:)];
		CCMenuItem *optionButton = [CCMenuItemFont itemFromString:@"Option" target:self selector:@selector(gotoOptionScene:)];
		//CCMenuItem *logo = [CCMenuItemImage itemFromNormalImage:@"Icon-72.png" selectedImage:@"Icon@2x.png" target:self selector:@selector(gotoGameScene:)];
		
		CCMenu *menu = [CCMenu menuWithItems:playButton, shopButton, optionButton, nil];
		[menu alignItemsVertically];
		
		[self addChild:menu];
		
		// Lets play some sound
		if (![[SimpleAudioEngine sharedEngine] isBackgroundMusicPlaying])
		{
			if ([[SimpleAudioEngine sharedEngine] backgroundMusicVolume] > 0.0f)
			{
				[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"title.mp3" loop:YES];
			}
		}
	}
	return self;
}

- (void) gotoGameScene:(id) sender
{
	[[CCDirector sharedDirector] replaceScene: [CCTransitionFade transitionWithDuration:0.5f scene:[GameScene scene]]];
}

- (void) gotoShopScene:(id) sender
{
	[[CCDirector sharedDirector] replaceScene: [CCTransitionFade transitionWithDuration:0.5f scene:[ShopScene scene]]];
}

- (void) gotoOptionScene:(id) sender
{
	[[CCDirector sharedDirector] replaceScene: [CCTransitionFade transitionWithDuration:0.5f scene:[OptionScene scene]]];
}

- (void) dealloc
{
	[super dealloc];
}

//---------------------------------------------------------------------------------------------------------------------
- (void) requestFreshAd:(id) sender
{
	[adMobAd requestFreshAd];
}

- (void) onEnter
{
	[super onEnter];
}

- (void) onEnterTransitionDidFinish 		
{
	[super onEnterTransitionDidFinish];
	
	myAdDelegate = [[AdDelegate alloc] init];
	
	adMobAd = [AdMobView requestAdOfSize:ADMOB_SIZE_320x48 withDelegate:myAdDelegate];
	adMobAd.frame = CGRectMake(0, 480, 320, 48); // Hide at the bottom
	[adMobAd retain];
	
	UIViewController *viewController = [myAdDelegate currentViewControllerForAd:adMobAd];
	[viewController.view addSubview:adMobAd];
	
	// Request fresh Ad every 15 seconds
	[self schedule: @selector(requestFreshAd:) interval:15];
}

- (void) onExit
{
	[myAdDelegate hideAdMobView:adMobAd];
	
	[adMobAd removeFromSuperview];
	[adMobAd release];
	
	[myAdDelegate release];
	
	[super onExit];
}

@end
